package com.example.front.controller;

import com.example.front.service.IOrderService;
import com.example.front.service.UserService;
import com.example.gok_mall.entity.User;
import com.example.gok_mall.reponseData.ResultData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/order")
@ResponseBody
public class OrderController {

    private static final Logger logger = LoggerFactory.getLogger(OrderController.class);

    @Autowired
    private IOrderService iOrderService;

    @Autowired
    private UserService userService;


    //创建订单接口
    @PostMapping("/create.do")
    public ResultData create(HttpSession session, Integer shippingId) {
        User user = (User) session.getAttribute("userInfo");
        if (user == null) {
            return ResultData.fail("用户未登录，请登录后再试");
        }
        if (userService.checkAdminRole(user) != null) {
            return iOrderService.createOrder(user.getId(), shippingId);
        } else {
            return ResultData.fail("创建订单失败");
        }
    }

    //获取购物车接口 (已经选中打算结算的购物车中商品的信息接口)
    @GetMapping("/get_order_cart_product.do")
    public ResultData getOrderCartProduct(HttpSession session) {
        User user = (User) session.getAttribute("userInfo");
        if (user == null) {
            return ResultData.fail("用户未登录，请登录后再试");
        }
        if (userService.checkAdminRole(user) != null) {
            return iOrderService.getOrderCartProduct(user.getId());
        }else {
            return ResultData.fail("获取购物车接口失败");
        }
    }

    //查询订单详情的接口
    @GetMapping("/detail.do")
    public ResultData detail(HttpSession session, Long orderNo) {
        User user = (User) session.getAttribute("userInfo");
        if (user == null) {
            return ResultData.fail("用户未登录，请登录后再试");
        }
        if (userService.checkAdminRole(user) != null) {
            return iOrderService.getOrderDetail(user.getId(), orderNo);
        } else {
            return ResultData.fail("获取购物车订单详情失败");
        }
    }

    //个人中心查看订单列表接口
    @GetMapping("/list.do")
    public ResultData list(HttpSession session, @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,@RequestParam(value = "pageSize",defaultValue = "10") int pageSize) {
        User user = (User) session.getAttribute("userInfo");
        if (user == null) {
            return ResultData.fail("用户未登录，请登录后再试");
        }
        if (userService.checkAdminRole(user) != null) {
        return iOrderService.getOrderList(user.getId(),pageNum,pageSize);
        }else {
                return ResultData.fail("获取购物车订单列表失败");
            }
        }

    //取消订单接口
    @PostMapping("/cancel.do")
    public ResultData cancel(HttpSession session, Long orderNo) {
        User user = (User) session.getAttribute("userInfo");
        if (user == null) {
            return ResultData.fail("用户未登录，请登录后再试");
        }
        if (userService.checkAdminRole(user) != null) {
        return iOrderService.cancel(user.getId(),orderNo);
        } else {
                return ResultData.fail("取消订单失败");
            }
        }
    }

